package com.atguigu.gmall.realtime.dws.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.common.base.BaseApp;
import com.atguigu.gmall.realtime.common.bean.TradeOrderBean;
import com.atguigu.gmall.realtime.common.constant.Constant;
import com.atguigu.gmall.realtime.common.function.BeanToJsonMapFunction;
import com.atguigu.gmall.realtime.common.util.DateFormatUtil;
import com.atguigu.gmall.realtime.common.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.functions.RuntimeContext;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * 从 Kafka订单明细主题读取数据，统计当日下单独立用户数和首次下单用户数，封装为实体类，写入Doris。
 */
public class DwsTradeOrderWindow extends BaseApp {
    public static void main(String[] args) {
        new DwsTradeOrderWindow().start(10028,
                4,
                "dws_trade_order_window",
                Constant.TOPIC_DWD_TRADE_ORDER_DETAIL);
    }

    @Override
    public void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        // 从 Kafka订单明细主题读取数据
        // 转换数据结构
        // Kafka 订单明细主题的数据是通过 Kafka-Connector 从订单预处理主题读取后进行过滤获取的，
        // Kafka-Connector 会过滤掉主题中的 null 数据，因此订单明细主题不存在为 null 的数据，直接转换数据结构即可。
        SingleOutputStreamOperator<JSONObject> mapDs = stream.map(JSON::parseObject);
        // mapDs.print("mapDs");


        // 设置水位线
        SingleOutputStreamOperator<JSONObject> watermarksDs = mapDs.assignTimestampsAndWatermarks(
                WatermarkStrategy.<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(3L))
                        .withTimestampAssigner((jsonObj, ts) -> jsonObj.getLong("ts") * 1000)
                        .withIdleness(Duration.ofSeconds(120L))
        );

        // watermarksDs.print("watermarksDs");


        // 按照用户 id 分组
        SingleOutputStreamOperator<TradeOrderBean> processDs = watermarksDs.keyBy(jsonObject -> jsonObject.getString("user_id"))
                .process(
                        new KeyedProcessFunction<String, JSONObject, TradeOrderBean>() {
                            private ValueState<String> lastOrderDateState;

                            @Override
                            public void open(Configuration parameters) throws Exception {
                                RuntimeContext runtimeContext = getRuntimeContext();
                                lastOrderDateState = runtimeContext.getState(
                                        new ValueStateDescriptor<String>("lastDateStateDesc", Types.STRING)
                                );
                            }

                            @Override
                            public void processElement(JSONObject jsonObject, KeyedProcessFunction<String, JSONObject, TradeOrderBean>.Context context, Collector<TradeOrderBean> collector) throws Exception {
                                // 运用 Flink 状态编程，在状态中维护用户末次下单日期。
                                // 若末次下单日期为 null，则将首次下单用户数和下单独立用户数均置为 1；
                                // 否则首次下单用户数置为 0，判断末次下单日期是否为当日，如果不是当日则下单独立用户数置为 1，否则置为 0。最后将状态中的下单日期更新为当日。
                                // 开窗、聚合
                                String lastOrderDate = lastOrderDateState.value();
                                String today = DateFormatUtil.tsToDate(jsonObject.getLong("ts") * 1000);
                                Long orderUniqueUserCount = 0L;
                                Long orderNewUserCount = 0L;
                                Long ts = jsonObject.getLong("ts");
                                if (lastOrderDate == null) {
                                    orderNewUserCount = 1L;
                                    orderUniqueUserCount = 1L;
                                } else {
                                    if (!lastOrderDate.equals(today)) {
                                        orderUniqueUserCount = 1L;
                                    }
                                }

                                lastOrderDateState.update(today);
                                if (orderNewUserCount + orderUniqueUserCount != 0) {
                                    collector.collect(
                                            TradeOrderBean.builder()
                                                    .stt("")
                                                    .edt("")
                                                    .curDate("")
                                                    .orderUniqueUserCount(orderUniqueUserCount)
                                                    .orderNewUserCount(orderNewUserCount)
                                                    .ts(ts)
                                                    .build()
                                    );
                                }
                            }
                        }
                );
        // processDs.print("processDs");


        // 补充窗口起始时间和结束时间字段，ts 字段置为当前系统时间戳。
        SingleOutputStreamOperator<TradeOrderBean> reduceDs = processDs.windowAll(TumblingEventTimeWindows.of(Time.seconds(5L)))
                .reduce(
                        new ReduceFunction<TradeOrderBean>() {
                            @Override
                            public TradeOrderBean reduce(TradeOrderBean t1, TradeOrderBean t2) throws Exception {
                                t1.setOrderNewUserCount(t1.getOrderNewUserCount() + t2.getOrderNewUserCount());
                                t1.setOrderUniqueUserCount(t1.getOrderUniqueUserCount() + t2.getOrderUniqueUserCount());
                                return t1;
                            }
                        },
                        new ProcessAllWindowFunction<TradeOrderBean, TradeOrderBean, TimeWindow>() {
                            @Override
                            public void process(ProcessAllWindowFunction<TradeOrderBean, TradeOrderBean, TimeWindow>.Context context, Iterable<TradeOrderBean> iterable, Collector<TradeOrderBean> collector) throws Exception {
                                TradeOrderBean bean = iterable.iterator().next();
                                bean.setStt(DateFormatUtil.tsToDateTime(context.window().getStart()));
                                bean.setEdt(DateFormatUtil.tsToDateTime(context.window().getEnd()));
                                bean.setCurDate(DateFormatUtil.tsToDate(context.window().getStart()));
                                collector.collect(bean);
                            }
                        }
                );
        // reduceDs.print("reduceDs");


        // 写出到Doris。
        reduceDs.map(
                new BeanToJsonMapFunction<>()
        ).sinkTo(FlinkSinkUtil.getDorisSink(Constant.DORIS_DATABASE, Constant.DWS_TRADE_ORDER_WINDOW));


    }
}
